package com.example.demo.utils;

import java.util.Calendar;

public class TimeManager {
	
	public static boolean timeEnable(char T,Calendar time){
		
		//Calendar time=Calendar.getInstance();
		switch(T){
		case 'D':
			return true;
		case 'W':
			{
				if(time.get(Calendar.DAY_OF_WEEK)==1)
					return true;
				else
					return false;
			}
		case 'M':
			{
				if(time.get(Calendar.DAY_OF_MONTH)==1)
					return true;
				else
					return false;
			}
		case 'Y':
			{
				if((time.get(Calendar.DAY_OF_MONTH)==1)&&(time.get(Calendar.MONTH)==0))
					return true;
				else
					return false;
			}
		default:
			return false;
		}

	}
	
	public static long[] timeChoice(Calendar time,char T){
		switch(T){
		case 'D':
			return getTimeDaily(time);
		case 'W':
			return getTimeWeekly(time);
		case 'M':
			return getTimeMonthly(time);
		case 'Y':
			return getTimeYearly(time);
            case 'T':
				return getTimeThreeMonth(time);


		default:
			return null;
		}
	}

	private static long[] getTimeThreeMonth(Calendar time) {
        long startTime;
        long endTime;
        long saveTime;
        long timeParam[]=new long[3];
        //Calendar time=Calendar.getInstance();
        endTime=time.getTimeInMillis();
        //time.set(Calendar.DAY_OF_MONTH,1);
        //time.set(Calendar.HOUR_OF_DAY,0);
        //time.set(Calendar.MINUTE,0);
        //time.set(Calendar.SECOND,0);
        //time.set(Calendar.MILLISECOND,0);


        time.add(Calendar.MONTH,-3);
        startTime=time.getTimeInMillis();

        time.set(Calendar.HOUR_OF_DAY,1);
        saveTime=time.getTimeInMillis();

        timeParam[0]=startTime;
        timeParam[1]=endTime;
        timeParam[2]=saveTime;
        return timeParam;

	}

	private static long[] getTimeWeekly(Calendar time){
		long startTime;
		long endTime;
		long saveTime;
		long timeParam[]=new long[3];
		//Calendar time=Calendar.getInstance();
		
		time.set(Calendar.DAY_OF_WEEK,1);
		time.set(Calendar.HOUR_OF_DAY,0);
		time.set(Calendar.MINUTE,0);
		time.set(Calendar.SECOND,0);
		time.set(Calendar.MILLISECOND,0);
		endTime=time.getTimeInMillis();
		
		time.add(Calendar.WEEK_OF_YEAR,-1);
		startTime=time.getTimeInMillis();
		
		time.set(Calendar.HOUR_OF_DAY,1);
		saveTime=time.getTimeInMillis();
		
		timeParam[0]=startTime;
		timeParam[1]=endTime;
		timeParam[2]=saveTime;
		return timeParam;
	}
	
	private static long[] getTimeMonthly(Calendar time){
		long startTime;
		long endTime;
		long saveTime;
		long timeParam[]=new long[3];
		//Calendar time=Calendar.getInstance();
		
		time.set(Calendar.DAY_OF_MONTH,1);
		time.set(Calendar.HOUR_OF_DAY,0);
		time.set(Calendar.MINUTE,0);
		time.set(Calendar.SECOND,0);
		time.set(Calendar.MILLISECOND,0);
		endTime=time.getTimeInMillis();
		
		time.add(Calendar.MONTH,-1);
		startTime=time.getTimeInMillis();
		
		time.set(Calendar.HOUR_OF_DAY,1);
		saveTime=time.getTimeInMillis();
		
		timeParam[0]=startTime;
		timeParam[1]=endTime;
		timeParam[2]=saveTime;
		return timeParam;
	}
	private static long[] getTimeDaily(Calendar time){
		long startTime;
		long endTime;
		long saveTime;
		long timeParam[]=new long[3];
		//Calendar time=Calendar.getInstance();
		
		time.set(Calendar.HOUR_OF_DAY,0);
		time.set(Calendar.MINUTE,0);
		time.set(Calendar.SECOND,0);
		time.set(Calendar.MILLISECOND,0);
		endTime=time.getTimeInMillis();
		
		time.add(Calendar.DATE,-1);
		startTime=time.getTimeInMillis();
		
		time.set(Calendar.HOUR_OF_DAY,1);
		saveTime=time.getTimeInMillis();
		
		timeParam[0]=startTime;
		timeParam[1]=endTime;
		timeParam[2]=saveTime;
		return timeParam;
	}
	
	private static long[] getTimeYearly(Calendar time){
		long startTime;
		long endTime;
		long saveTime;
		long timeParam[]=new long[3];
		//Calendar time=Calendar.getInstance();
		
		time.set(Calendar.MONTH,0);		
		time.set(Calendar.DAY_OF_MONTH,1);
		time.set(Calendar.HOUR_OF_DAY,0);
		time.set(Calendar.MINUTE,0);
		time.set(Calendar.SECOND,0);
		time.set(Calendar.MILLISECOND,0);
		endTime=time.getTimeInMillis();
		
		time.add(Calendar.YEAR,-1);
		startTime=time.getTimeInMillis();
		
		time.set(Calendar.HOUR_OF_DAY,1);
		saveTime=time.getTimeInMillis();
		
		timeParam[0]=startTime;
		timeParam[1]=endTime;
		timeParam[2]=saveTime;
		return timeParam;
	}
	
	
//	public static void main(String[] args) {
//		TimeManager tm=new TimeManager();
//		boolean flag=tm.timeEnable('R');
//		System.out.println(flag);
//	}

}
